﻿
using System.Linq;
using System.Collections.Generic;

using Gola.VN.Scb.Web.Common.Entities;

namespace Gola.VN.Scb.Web.Dal
{
    class MemoryDao
    {
        private IEnumerable<ProductEntity> _products;
        private IEnumerable<ProductEntity> _pendingProducts;
        private List<CategoryEntity> _categoriesAsTree;
        private IEnumerable<CategoryEntity> _categoriesAsFlat;
        private IEnumerable<ProviderEntity> _providers;
        private int _sysInfo;
        private IEnumerable<LocationEntity> _locations;
        private Dictionary<string, SearchHistoryEntity> _searchHistories;

        #region 1. product
        internal IEnumerable<ProductEntity> GetProducts()
        {
            return this._products;
        }

        internal void SetProducts(IEnumerable<ProductEntity> products)
        {
            this._products = products;
        }

        internal void InsertProduct(ProductEntity product)
        {
            List<ProductEntity> products = _pendingProducts != null ? _pendingProducts.ToList() : new List<ProductEntity>();
            products.Add(product);

            _pendingProducts = products;
        }

        internal void UpdateProduct(ProductEntity product)
        {
            List<ProductEntity> products = _pendingProducts != null ? _pendingProducts.ToList() : new List<ProductEntity>();
            var updateProduct = products.Find(q => q.Id == product.Id);
            if (updateProduct != null)
            {
                updateProduct.ViewCount = product.ViewCount;//just update view count
            }

        }

        #endregion

        #region 2. category
        internal void SetCategoriesAsTree(List<CategoryEntity> categoriesAsTree)
        {
            _categoriesAsTree = categoriesAsTree;
        }

        internal List<CategoryEntity> GetCategoriesAsTree()
        {
            return _categoriesAsTree;
        }

        internal void SetCategoriesAsFlat(IEnumerable<CategoryEntity> categoriesAsFlat)
        {
            _categoriesAsFlat = categoriesAsFlat;
        }

        internal IEnumerable<CategoryEntity> GetCategoriesAsFlat()
        {
            return _categoriesAsFlat;
        }
        #endregion

        #region 3. provider
        internal IEnumerable<ProviderEntity> GetProviders()
        {
            return _providers;
        }

        internal void SetProviders(IEnumerable<ProviderEntity> providers)
        {
            _providers = providers;
        }
        #endregion

        #region 4. sysinfo
        internal int GetSysInfo()
        {
            return _sysInfo;
        }

        internal void SetSysInfo(int sysInfo)
        {
            _sysInfo = sysInfo;
        }
        #endregion

        #region 5. location
        internal IEnumerable<LocationEntity> GetLocations()
        {
            return _locations;
        }

        internal void SetLocations(IEnumerable<LocationEntity> locations)
        {
            _locations = locations;
        }
        #endregion

        #region 6. searchhistory
        internal List<SearchHistoryEntity> GetSearchHistories()
        {
            return _searchHistories.Values.ToList();
        }

        internal void UpdateSearchHistory(SearchHistoryEntity searchHistory)
        {
            if (_searchHistories.ContainsKey(searchHistory.Text))
            {
                _searchHistories[searchHistory.Text].Count += searchHistory.Count;
            }
            else
            {
                _searchHistories.Add(searchHistory.Text, searchHistory);
            }
        }

        internal void SetSearchHistories(List<SearchHistoryEntity> searchHistories)
        {
            if (_searchHistories == null || _searchHistories.Count() < 0)
            {
                _searchHistories = new Dictionary<string, SearchHistoryEntity>();

                if (searchHistories != null)
                {
                    foreach (SearchHistoryEntity dtoSearchHistory in searchHistories)
                    {
                        if (_searchHistories.ContainsKey(dtoSearchHistory.Text))
                        {
                            _searchHistories[dtoSearchHistory.Text].Count += dtoSearchHistory.Count;
                        }
                        else
                        {
                            _searchHistories.Add(dtoSearchHistory.Text, dtoSearchHistory);
                        }
                    }
                }
            }
        }
        #endregion
    }
}
